package com.imessage.action.api.client;

import com.bean.BusinessTaskDetail;
import com.bean.DeviceClient;
import com.bean.DeviceVirtual;
import com.imessage.jackson.JsonObject;
import com.imessage.type.DeviceVirtualState;
import com.imessage.utils.MessageUtils;
import com.imessage.vo.api.ApiClientVo;
import com.imessage.vo.api.ApiStateVo;
import io.ebean.Expr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/task/client")
public class ApiClientAction extends ApiClientBaseAction {


    @RequestMapping(value ="/get",method = RequestMethod.POST)//1.1 获取任务
    public Object get(@RequestBody(required = true) ApiClientVo apiClientVo) {
        JsonObject dataObject = new JsonObject();
        DeviceClient deviceClient =getDeviceClient(apiClientVo);
        //获得合适的虚拟机
        DeviceVirtual deviceVirtual=getRightDeviceVirtual(deviceClient,apiClientVo.getVirtualNum());
        //获得需要执行的任务
        BusinessTaskDetail businessTaskDetail=getRunBusinessTaskDetail(deviceVirtual);
        if(businessTaskDetail==null){
            businessTaskDetail=taskService.getNeedTaskBusinessTaskDetail();
            if(businessTaskDetail!=null){
                deviceVirtual=businessTaskDetail.getDeviceVirtual();
            }
        }
        if(businessTaskDetail==null){
            return MessageUtils.getErrorMessage("no task");
        }
        initGetObject(dataObject,deviceVirtual,businessTaskDetail);
        return MessageUtils.getSuccessMessage(dataObject);
    }


    @RequestMapping(value ="/result",method = RequestMethod.POST)//1.2 任务结果
    public Object result(@RequestBody(required = true) ApiClientVo apiClientVo) {
        JsonObject dataObject = new JsonObject();
        if(StringUtils.isNotBlank(apiClientVo.getCode())
                &&apiClientVo.getCode().equals("0")){
            return MessageUtils.getSuccessMessage(dataObject);
        }
        BusinessTaskDetail businessTaskDetail=baseService.findById(
                BusinessTaskDetail.class, apiClientVo.getTaskId());
        if(businessTaskDetail==null){
            return MessageUtils.getErrorMessage("data error");
        }
        taskService.updateFinishBusinessTaskDetail(businessTaskDetail,apiClientVo.getMsg());
        return MessageUtils.getSuccessMessage(dataObject);
    }


    @RequestMapping(value ="/state",method = RequestMethod.POST)//1.2 任务结果
    public Object state(@RequestBody(required = true) List<ApiStateVo> apiStateVoList) {
        JsonObject dataObject = new JsonObject();
        if(apiStateVoList==null||apiStateVoList.size()==0){
            return MessageUtils.getSuccessMessage(dataObject);
        }
        for(ApiStateVo apiStateVo:apiStateVoList){
            DeviceVirtual deviceVirtual = baseService.findObject(
                    DeviceVirtual.class, Expr.eq("virtualMac", apiStateVo.getMac().toUpperCase()));
            if(deviceVirtual==null){
                continue;
            }
            if(apiStateVo.getState().equalsIgnoreCase("1")){//1=>运行
                deviceVirtual.setState(DeviceVirtualState.run);
            }
            if(apiStateVo.getState().equalsIgnoreCase("0")){//0=>关机
                deviceVirtual.setState(DeviceVirtualState.shutdown);
            }
            if(apiStateVo.getState().equalsIgnoreCase("2")){//2=>暂停
                deviceVirtual.setState(DeviceVirtualState.pause);
            }
            baseService.updateObject(deviceVirtual);
        }
        return MessageUtils.getSuccessMessage(dataObject);
    }

}
